import { Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, Index } from "typeorm";
import { EntityModel } from "@midwayjs/orm";
import { Users } from "./Users.entity";
import { Child } from "./Child.entity";

@Index("user_child_unique", ["userId", "childId"], { unique: true })
@EntityModel("user_child", { schema: "heart_neighbor" })
export class UserChild {
  @PrimaryGeneratedColumn({ type: "int", name: "id", comment: "Primary Key" })
  id!: number;

  @Column("bigint", { name: "user_id", comment: "用户ID" })
  userId!: string;

  @Column("int", { name: "child_id", comment: "儿童ID" })
  childId!: number;

  @Column("tinyint", {
    name: "relation_type",
    comment: "关系类型，1=父亲，2=母亲，3=监护人，4=其他",
    width: 1,
    default: () => "'3'",
  })
  relationType!: number;

  @Column("timestamp", {
    name: "created_at",
    comment: "创建时间",
    default: () => "CURRENT_TIMESTAMP",
  })
  createdAt!: Date;

  // 多对一关系：多个关联记录对应一个用户
  @ManyToOne(() => Users, (user) => user.userChildren, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "user_id" })
  user!: Users;

  // 多对一关系：多个关联记录对应一个儿童
  @ManyToOne(() => Child, (child) => child.userChildren, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "child_id" })
  child!: Child;
}